/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package entidade.controller.JPA;


import entidade.controller.JPA.exceptions.IllegalOrphanException;
import entidade.controller.JPA.exceptions.NonexistentEntityException;
import entidade.controller.JPA.exceptions.RollbackFailureException;
import entidade.persistencia.OperacaoExtraTipo;
import entidade.persistencia.OpercaoExtra;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.transaction.UserTransaction;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author victorinox
 */
public class OperacaoExtraTipoJpaController {
    @Resource
    private UserTransaction utx = null;
    @PersistenceUnit(unitName = "globalimage2PU")
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(OperacaoExtraTipo operacaoExtraTipo) throws RollbackFailureException, Exception {
        if (operacaoExtraTipo.getOpercaoExtraCollection() == null) {
            operacaoExtraTipo.setOpercaoExtraCollection(new ArrayList<OpercaoExtra>());
        }
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            List<OpercaoExtra> attachedOpercaoExtraCollection = new ArrayList<OpercaoExtra>();
            for (OpercaoExtra opercaoExtraCollectionOpercaoExtraToAttach : operacaoExtraTipo.getOpercaoExtraCollection()) {
                opercaoExtraCollectionOpercaoExtraToAttach = em.getReference(opercaoExtraCollectionOpercaoExtraToAttach.getClass(), opercaoExtraCollectionOpercaoExtraToAttach.getOperacaoExtraId());
                attachedOpercaoExtraCollection.add(opercaoExtraCollectionOpercaoExtraToAttach);
            }
            operacaoExtraTipo.setOpercaoExtraCollection(attachedOpercaoExtraCollection);
            em.persist(operacaoExtraTipo);
            for (OpercaoExtra opercaoExtraCollectionOpercaoExtra : operacaoExtraTipo.getOpercaoExtraCollection()) {
                OperacaoExtraTipo oldOperacaoExtraTipoIdOfOpercaoExtraCollectionOpercaoExtra = opercaoExtraCollectionOpercaoExtra.getOperacaoExtraTipoId();
                opercaoExtraCollectionOpercaoExtra.setOperacaoExtraTipoId(operacaoExtraTipo);
                opercaoExtraCollectionOpercaoExtra = em.merge(opercaoExtraCollectionOpercaoExtra);
                if (oldOperacaoExtraTipoIdOfOpercaoExtraCollectionOpercaoExtra != null) {
                    oldOperacaoExtraTipoIdOfOpercaoExtraCollectionOpercaoExtra.getOpercaoExtraCollection().remove(opercaoExtraCollectionOpercaoExtra);
                    oldOperacaoExtraTipoIdOfOpercaoExtraCollectionOpercaoExtra = em.merge(oldOperacaoExtraTipoIdOfOpercaoExtraCollectionOpercaoExtra);
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(OperacaoExtraTipo operacaoExtraTipo) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            OperacaoExtraTipo persistentOperacaoExtraTipo = em.find(OperacaoExtraTipo.class, operacaoExtraTipo.getOperacaoExtraTipoId());
            List<OpercaoExtra> opercaoExtraCollectionOld = persistentOperacaoExtraTipo.getOpercaoExtraCollection();
            List<OpercaoExtra> opercaoExtraCollectionNew = operacaoExtraTipo.getOpercaoExtraCollection();
            List<String> illegalOrphanMessages = null;
            for (OpercaoExtra opercaoExtraCollectionOldOpercaoExtra : opercaoExtraCollectionOld) {
                if (!opercaoExtraCollectionNew.contains(opercaoExtraCollectionOldOpercaoExtra)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain OpercaoExtra " + opercaoExtraCollectionOldOpercaoExtra + " since its operacaoExtraTipoId field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<OpercaoExtra> attachedOpercaoExtraCollectionNew = new ArrayList<OpercaoExtra>();
            for (OpercaoExtra opercaoExtraCollectionNewOpercaoExtraToAttach : opercaoExtraCollectionNew) {
                opercaoExtraCollectionNewOpercaoExtraToAttach = em.getReference(opercaoExtraCollectionNewOpercaoExtraToAttach.getClass(), opercaoExtraCollectionNewOpercaoExtraToAttach.getOperacaoExtraId());
                attachedOpercaoExtraCollectionNew.add(opercaoExtraCollectionNewOpercaoExtraToAttach);
            }
            opercaoExtraCollectionNew = attachedOpercaoExtraCollectionNew;
            operacaoExtraTipo.setOpercaoExtraCollection(opercaoExtraCollectionNew);
            operacaoExtraTipo = em.merge(operacaoExtraTipo);
            for (OpercaoExtra opercaoExtraCollectionNewOpercaoExtra : opercaoExtraCollectionNew) {
                if (!opercaoExtraCollectionOld.contains(opercaoExtraCollectionNewOpercaoExtra)) {
                    OperacaoExtraTipo oldOperacaoExtraTipoIdOfOpercaoExtraCollectionNewOpercaoExtra = opercaoExtraCollectionNewOpercaoExtra.getOperacaoExtraTipoId();
                    opercaoExtraCollectionNewOpercaoExtra.setOperacaoExtraTipoId(operacaoExtraTipo);
                    opercaoExtraCollectionNewOpercaoExtra = em.merge(opercaoExtraCollectionNewOpercaoExtra);
                    if (oldOperacaoExtraTipoIdOfOpercaoExtraCollectionNewOpercaoExtra != null && !oldOperacaoExtraTipoIdOfOpercaoExtraCollectionNewOpercaoExtra.equals(operacaoExtraTipo)) {
                        oldOperacaoExtraTipoIdOfOpercaoExtraCollectionNewOpercaoExtra.getOpercaoExtraCollection().remove(opercaoExtraCollectionNewOpercaoExtra);
                        oldOperacaoExtraTipoIdOfOpercaoExtraCollectionNewOpercaoExtra = em.merge(oldOperacaoExtraTipoIdOfOpercaoExtraCollectionNewOpercaoExtra);
                    }
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = operacaoExtraTipo.getOperacaoExtraTipoId();
                if (findOperacaoExtraTipo(id) == null) {
                    throw new NonexistentEntityException("The operacaoExtraTipo with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            OperacaoExtraTipo operacaoExtraTipo;
            try {
                operacaoExtraTipo = em.getReference(OperacaoExtraTipo.class, id);
                operacaoExtraTipo.getOperacaoExtraTipoId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The operacaoExtraTipo with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<OpercaoExtra> opercaoExtraCollectionOrphanCheck = operacaoExtraTipo.getOpercaoExtraCollection();
            for (OpercaoExtra opercaoExtraCollectionOrphanCheckOpercaoExtra : opercaoExtraCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This OperacaoExtraTipo (" + operacaoExtraTipo + ") cannot be destroyed since the OpercaoExtra " + opercaoExtraCollectionOrphanCheckOpercaoExtra + " in its opercaoExtraCollection field has a non-nullable operacaoExtraTipoId field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(operacaoExtraTipo);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<OperacaoExtraTipo> findOperacaoExtraTipoEntities() {
        return findOperacaoExtraTipoEntities(true, -1, -1);
    }

    public List<OperacaoExtraTipo> findOperacaoExtraTipoEntities(int maxResults, int firstResult) {
        return findOperacaoExtraTipoEntities(false, maxResults, firstResult);
    }

    private List<OperacaoExtraTipo> findOperacaoExtraTipoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from OperacaoExtraTipo as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public OperacaoExtraTipo findOperacaoExtraTipo(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(OperacaoExtraTipo.class, id);
        } finally {
            em.close();
        }
    }

    public int getOperacaoExtraTipoCount() {
        EntityManager em = getEntityManager();
        try {
            return ((Long) em.createQuery("select count(o) from OperacaoExtraTipo as o").getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
